#pragma once
#ifndef __MATERIAL__TEXT_EDIT__HH__
#define __MATERIAL__TEXT_EDIT__HH__

#include <QtGui/QDialog>
#include <LitEditorPrerequisites.h>
#include <LitFileSystem.h>
#include <LitEditorFileDialog.h>
#include <qplaintextedit.h>
#include <qsyntaxhighlighter.h>
#include "LitEditorLexer.h"
#include "LitEditorPolice.h"

namespace Lit
{
	namespace Editor
	{
		namespace Material
		{
			class TextEdit : public QPlainTextEdit
			{
				Q_OBJECT
			public:
				class Margin;
				class SyntaxHighlighter;

				TextEdit( QWidget * parent );
				~TextEdit( );
				
				void keyPressEvent( QKeyEvent * evt );

				bool isModified() const{ return document()->isModified(); }
				void setModified( bool state ){ document()->setModified( state ); }

				void lineNumberAreaPaintEvent(QPaintEvent *evt);
				int lineNumberAreaWidth();
				
			private:
				void indentSelection();
				void unIndentSelection();

				void onTabKey(QKeyEvent*);
				void onReturnKey(QKeyEvent*);
				void onBackspaceKey(QKeyEvent*);

			protected:
				void resizeEvent(QResizeEvent *evt);

			private slots:
				void updateLineNumberAreaWidth(int newBlockCount);
				void highlightCurrentLine();
				void updateLineNumberArea(const QRect &, int);

			protected:
				Margin * mMargin;
				SyntaxHighlighter * mSyntaxer;
			};
			class TextEdit::SyntaxHighlighter : public QSyntaxHighlighter
			{ 
				Q_OBJECT

			public:
				SyntaxHighlighter(QTextDocument *parent = 0);

			protected:
				void highlightBlock(const QString &text);

			private:
				Lexer::Keywords::HighlightingRuleList mRules;
			};
			class TextEdit::Margin : public QWidget
			{
			public:
				Margin(TextEdit *edt);
				virtual ~Margin();

				QSize sizeHint() const;

			protected:
				void paintEvent(QPaintEvent *event);

			private:
				TextEdit *mEditor;
			};
		};
	};
};

#endif